﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using BikeSystem.Models;
using BikeSystem.BLL;

namespace BikeUI
{
    public partial class FormAccounts : FormBase
    {
        AccountManager accountManager = new AccountManager();
        public FormAccounts()
        {
            InitializeComponent();


            cbRole.Items.Add(ConstData.ACCOUNT_ROLE_OPERATOR);
            cbRole.Items.Add(ConstData.ACCOUNT_ROLE_ADMIN);
            cbRole.Items.Add(ConstData.ACCOUNT_ROLE_BOSS);

            loadAccounts();

            UCButtonGroup.KeyComponent = txtLoginId;
            UCButtonGroup.EditableGroupBox = gbxAccount;
            UCButtonGroup.BindingSource = accountBindingSource;

            UCButtonGroup.EditableGroupBox.Enabled = false;
        }

        protected override void btnNew_Click(object sender, EventArgs e)
        {

            Account account = (Account)accountBindingSource.Current;
            account.IsNew = true;

        }

        protected override void btnUpdate_Click(object sender, EventArgs e)
        {

            Account account = (Account)accountBindingSource.Current;

            if (!account.IsNew)
            {
                account.IsUpdated = true;
            }


        }

        protected override void btnSave_Click(object sender, EventArgs e)
        {

            errorProviderClear();

            txtAlphaNumericString_Validated(txtLoginId);
            txtNumericString_Validated(txtAge);

            if (!UCButtonGroup.HasError)
            {
                Account account = (Account)accountBindingSource.Current;
                if (account.IsNew)
                {
                    accountManager.InsertAccount(account);
                }
                else if (account.IsUpdated)
                {
                    accountManager.UpdateAccount(account);
                }
                loadAccounts();

            }


        }

        protected override void btnDelete_Click(object sender, EventArgs e)
        {

            Account account = (Account)accountBindingSource.Current;

            if (!account.IsNew)
            {
                accountManager.DeleteAccount(account.LoginId);
            }

            loadAccounts();

        }

        private void cbxOperator_CheckedChanged(object sender, EventArgs e)
        {

            loadAccounts();
        }

        private void cbxBoss_CheckedChanged(object sender, EventArgs e)
        {

            loadAccounts();
        }

        private void cbxAdmin_CheckedChanged(object sender, EventArgs e)
        {

            loadAccounts();
        }

        private void loadAccounts() {

            IList<Account> accounts = accountManager.GetAllAccounts();

            IList<Account> listAccounts = new List<Account>();

            foreach(Account act in accounts){
                if (cbxOperator.Checked && ConstData.ACCOUNT_ROLE_OPERATOR.Equals(act.Role))
                {
                    listAccounts.Add(act);
                }

                if (cbxAdmin.Checked && ConstData.ACCOUNT_ROLE_ADMIN.Equals(act.Role))
                {
                    listAccounts.Add(act);
                }

                if (cbxBoss.Checked && ConstData.ACCOUNT_ROLE_BOSS.Equals(act.Role))
                {
                    listAccounts.Add(act);
                }
            
            }

            accountBindingSource.DataSource = listAccounts;

        
        }


    }
}
